﻿Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Audio
Imports Runa.Resources.ResourceManager
Imports Microsoft.Xna.Framework.Media

Namespace Global.Runa.Resources.ContentManagers

    Public MustInherit Class BasicContentManager
        Inherits Microsoft.Xna.Framework.Content.ContentManager

        Private m_resourcePool As New List(Of IDisposable)

        Public Sub New(serviceProvider As System.IServiceProvider)
            MyBase.New(serviceProvider)
        End Sub

        Public Sub New(serviceProvider As System.IServiceProvider, rootDirectory As String)
            MyBase.New(serviceProvider, rootDirectory)
        End Sub

        Public Overrides Function Load(Of T)(assetName As String) As T
#If NETFX_CORE Then
            If Me.LoadedAssets.ContainsKey(assetName) Then
                If TypeOf Me.LoadedAssets(assetName) Is T Then
                    Return Me.LoadedAssets(assetName)
                End If
            End If
#End If
            If GetType(T) Is GetType(String) Then
                Using tr As TextReader = New StreamReader(Me.OpenStream(assetName))
                    Dim result = tr.ReadToEnd()
                    Return GenericCast(Of String, T)(result)
                End Using
                'ElseIf GetType(T) Is GetType(Video) Then
                '    Dim v = MyBase.Load(Of T)(assetName)
                '    Dim typ = GetType(T)
                '    Dim pis = typ.GetFields(Reflection.BindingFlags.NonPublic)
                '    For Each pi In pis
                '        If pi.Name = "filename" Then
                '            Dim fn = pi.GetValue(v)
                '            Return v
                '        End If
                '    Next
            Else
                Dim result = MyBase.Load(Of T)(assetName)
                Return result
            End If
        End Function

        Public Function LoadSpriteFont(ByVal assetName As String) As FontObject
            Return New FontObject(assetName, assetName, Me.Load(Of SpriteFont)(assetName))
        End Function

        Public Function LoadVectorFont(ByVal assetName As String) As VectorFontObject
            Return New VectorFontObject(assetName, assetName, Me.Load(Of VectorFontObject)(assetName))
        End Function

        Public Function LoadTexture2D(ByVal assetName As String) As Texture2DObject
            Return New Texture2DObject(assetName, assetName, Me.Load(Of Texture2D)(assetName))
        End Function

        Public Function LoadAudio(ByVal assetName As String) As AudioObject
            'Return New AudioObject(assetName, assetName, Me.ReadAsset(Of SoundEffect)(assetName,
            '    Sub(obj)
            '        Me.m_resourcePool.Add(obj)
            '    End Sub))
            Try
                Return New AudioObject(assetName, assetName, Me.Load(Of SoundEffect)(assetName))
            Catch ex As Exception
                Return AudioObject.LoadFromStream(assetName, assetName, Me.OpenStream(assetName))
            End Try
        End Function

        Public Function LoadVideo(ByVal assetName As String) As VideoObject
            Return New VideoObject(assetName, assetName, Me.Load(Of Video)(assetName))
        End Function

        Public Function LoadAllText(ByVal assetName As String) As String
            If Me.Exists(assetName) Then
                Using fs = Me.OpenStream(assetName)
                    Using tr As TextReader = New StreamReader(fs)
                        Return tr.ReadToEnd()
                        tr.Close()
                    End Using
                    fs.Close()
                End Using
            Else
                Throw New FileNotFoundException(String.Format("File {0} not found.", assetName))
            End If
        End Function

        Public Function LoadAllBytes(ByVal assetName As String) As Byte()
            If Me.Exists(assetName) Then
                Using fs = Me.OpenStream(assetName)
                    Using ms As New MemoryStream()
                        fs.CopyTo(ms)
                        ms.Flush()
                        Return ms.ToArray()
                    End Using
                End Using
            Else
                Throw New FileNotFoundException(String.Format("File {0} not found.", assetName))
            End If
        End Function

        Public Overrides Sub Unload()
            For Each obj In Me.m_resourcePool
                obj.Dispose()
            Next
            Me.m_resourcePool.Clear()
            MyBase.Unload()
        End Sub

        Public MustOverride Function Exists(ByVal assetName As String) As Boolean
        Protected MustOverride Overrides Function OpenStream(assetName As String) As System.IO.Stream

    End Class

End Namespace